package com.it.blog.tools;

import java.time.LocalDate;
import java.time.Month;

/**
 * @Author: 羡羡
 * @Date: 2022/01/29/14:57
 *
 * 计算时间
 */
public class CompTime {
    public static String comtime(LocalDate start, LocalDate end){
        long betweenDay = end.toEpochDay() - start.toEpochDay();
        if (betweenDay < 0) {
            return "开始时间大于结束时间，所传参数不OK。。。。";
        }

        int year = end.getYear() - start.getYear();
        int month = end.getMonth().getValue() - start.getMonth().getValue();
        int day = end.getDayOfMonth() - start.getDayOfMonth();

        if (day < 0) {
            month = month - 1;
            boolean leapYear = end.getYear() % 4 == 0;
            if (end.getMonth() == Month.JANUARY) {
                leapYear = (end.getYear() - 1) % 4 == 0;
            }
            day = day + end.getMonth().minus(1).length(leapYear);
        }

        if (month < 0) {
            year = year - 1;
            month = month + 12;
        }
        String time=year+"年"+month+"月"+day+"天";
        return time;
    }
}
